package com.first.sso.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.first.common.pojo.TbMember;
import com.first.sso.vo.LoginVo;
import com.first.common.vo.Result;
import com.first.sso.service.SsoLogin;
import com.first.sso.vo.QianLoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @program: finrsteducation-parent
 * @description: 登录注册
 * @author: 张玉森
 * @create: 2023-05-31 15:50
 **/
@RestController
@RequestMapping("sso/admin")
@Api(tags = "客户登录界面")
public class Login {
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    private SsoLogin ssoLogin;
    @PostMapping("qianLogin")
    @ApiOperation("前台登陆界面")
    public Result qianLogin(@RequestBody QianLoginVo qianLoginVo){
//        System.out.println(qianLoginVo);
        TbMember tbMember = ssoLogin.qianLogin(qianLoginVo);
        if (tbMember!=null){
            if (tbMember.getIsDisabled().equals(0)){
                //表示成功
                String token = IdUtil.simpleUUID();
                tbMember.setPassword(null);
                redisTemplate.opsForValue().set("home::" + token, JSON.toJSONString(tbMember), 30, TimeUnit.MINUTES);
                LoginVo loginVo=new LoginVo();
                loginVo.setHomeToken(token);
                loginVo.setMobile(tbMember.getMobile());
                loginVo.setMemberId(tbMember.getId());
                loginVo.setIsTeacher(tbMember.getIsType()+"");
                return Result.success(loginVo);
            }else {

                return Result.fail("账号异常！请联系管理员");
            }
        }
       return Result.fail("账号或密码错误");
    }

}
